package cn.lili.controller.store;

import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.PageVO;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.goods.client.GoodsClient;
import cn.lili.modules.goods.entity.dto.ProxyGoodsOperationDTO;
import cn.lili.modules.search.client.EsGoodsSearchClient;
import cn.lili.modules.search.entity.dos.EsGoodsIndex;
import cn.lili.modules.search.entity.dos.EsGoodsRelatedInfo;
import cn.lili.modules.search.entity.dto.CustomSearchParams;
import cn.lili.modules.search.entity.dto.EsGoodsSearchDTO;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.elasticsearch.core.SearchPage;
import org.springframework.web.bind.annotation.*;

/**
 * 店铺端,商品接口
 *
 * @author pikachu
 * @since 2020-02-23 15:18:56
 */
@RestController
@Api(tags = "商家端端,供应商商品接口")
@RequestMapping("/supplier/store/goods")
@RequiredArgsConstructor
public class SupplierGoodsStoreController {

    private final GoodsClient goodsClient;

    private final EsGoodsSearchClient esGoodsSearchClient;

    @ApiOperation(value = "代理商品")
    @PostMapping("/proxy")
    public ResultMessage<Object> getGoodsRelatedByPageFromEs(String goodsId) {
        //供应商根据商品id，代理商品
        goodsClient.addSupplierGoods(goodsId);
        return ResultUtil.success();
    }


    /**
     * 批量代理
     */
    @PostMapping("/batchProxy")
    public ResultMessage<Object> batchProxy(@RequestParam String goodsIds) {
        for (String goodsId : goodsIds.split(",")) {
            //供应商根据商品id，代理商品
            goodsClient.addSupplierGoods(goodsId);
        }
        return ResultUtil.success();
    }

    @ApiOperation(value = "店铺修改代理商品")
    @PutMapping("/edit")
    public ResultMessage<Object> proxyUpdate(@RequestBody ProxyGoodsOperationDTO proxyGoodsOperationDTO) {
        System.out.println("proxyGoodsOperationDTO = " + JSONObject.toJSONString(proxyGoodsOperationDTO));
        goodsClient.editProxyGoods(proxyGoodsOperationDTO);
        return ResultUtil.success();
    }

    @ApiOperation(value = "从ES中获取商品信息")
    @GetMapping("/es")
    public ResultMessage<SearchPage<EsGoodsIndex>> getGoodsByPageFromEs(EsGoodsSearchDTO goodsSearchParams,
                                                                        PageVO pageVO) {
        pageVO.setNotConvert(true);
        CustomSearchParams customSearchParams = new CustomSearchParams();
        customSearchParams.setGoodsSearchParams(goodsSearchParams);
        customSearchParams.setPageVO(pageVO);
        SearchPage<EsGoodsIndex> esGoodsIndices = esGoodsSearchClient.customSearch(customSearchParams);
        return ResultUtil.data(esGoodsIndices);
    }

    @ApiOperation(value = "从ES中获取相关商品品牌名称，分类名称及属性")
    @GetMapping("/es/related")
    public ResultMessage<EsGoodsRelatedInfo> getGoodsRelatedByPageFromEs(EsGoodsSearchDTO goodsSearchParams,
                                                                         PageVO pageVO) {
        pageVO.setNotConvert(true);
        CustomSearchParams customSearchParams = new CustomSearchParams();
        customSearchParams.setGoodsSearchParams(goodsSearchParams);
        customSearchParams.setPageVO(pageVO);
        EsGoodsRelatedInfo selector = esGoodsSearchClient.related(customSearchParams);
        return ResultUtil.data(selector);
    }

}
